﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Net;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Theme_DL_UserControls_UserLogin : System.Web.UI.UserControl
{
    PowerCreator.LMS.Web.DL.User.WebLearner opUser = new PowerCreator.LMS.Web.DL.User.WebLearner();
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
        }

        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            pl_UserLogin.Visible = true;
            pl_UserInfo.Visible = false;
        }
        else
        {
            PowerCreator.LMS.Web.DL.User.LoginEntity login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login.VOLearner == null)
            {
                Session.Abandon();
                PowerCreator.LMS.Common.Script.Script.ReloadMe();

            }
            else
            {
                pl_UserLogin.Visible = false;
                pl_UserInfo.Visible = true;
                lbl_UserName.Text = login.UserName.Trim();
                lbl_UserReal.Text = login.RealName.Trim();
            }
        }
    }
    private void InitPage()
    {
        //btn_Login.Text = config.GetLanagerValueByKey("SysManagerLogin", PowerCreator.LMS.Language.LanagetItemType.Page);
       // btn_Registor.Text = config.GetLanagerValueByKey("SysManagerRegistor", PowerCreator.LMS.Language.LanagetItemType.Page);
        //btn_LoginOut.Text = "注销";
       // btn_Registor1.Text = config.GetLanagerValueByKey("SysManagerRegistor", PowerCreator.LMS.Language.LanagetItemType.Page);
    }

    private bool CheckPage()
    {
        if (string.IsNullOrEmpty(txt_code.Text))
        {

            PowerCreator.LMS.Common.Script.Script.Alert(this.Page, "msg", "请输入正确的五位验证码！");
            return false;
        }
        if (Session["CheckCode"] != null)
        {
            string s = Session["CheckCode"].ToString();
            if (s.ToLower() != txt_code.Text.ToLower())
            {
                //lblLoginMsg.Text = config.GetLanagerValueByKey("Login_msg_code2", PowerCreator.LMS.Language.LanagetItemType.Page);
                PowerCreator.LMS.Common.Script.Script.Alert(this.Page, "msg", "请输入正确的五位验证码！");
                txt_code.Text = "";
                //Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }
        if (string.IsNullOrEmpty(txt_UserName.Text))
        {

            PowerCreator.LMS.Common.Script.Script.Alert(this.Page, "msg", "请输入正确的帐户！");
            return false;
        }
        if (string.IsNullOrEmpty(txt_UserPwd.Text))
        {

            PowerCreator.LMS.Common.Script.Script.Alert(this.Page, "msg", "请输入正确的密码！");
            return false;
        }
        return true;

    }
    protected void btn_Login_Click(object sender, EventArgs e)
    {
        //登录
        //登录信息
        PowerCreator.LMS.Web.DL.User.LoginInfo info = new PowerCreator.LMS.Web.DL.User.LoginInfo();

        PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();

        if (!CheckPage()) return;

        info.LoginIp = PowerCreator.LMS.Common.Utils.IPUtils.GetIP;
        info.LoginTime = DateTime.Now;
        info.UserName = txt_UserName.Text;
        info.UserPwd = txt_UserPwd.Text;
        string UserLocation = GetUserLocation();

        if (opUser.Login(info, ref login, ConfigurationManager.AppSettings["CheckLoginIP"].ToString(), UserLocation))
        {
            Session.Add(PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO, login);
            //opUser.UpdateOnlineUserList();
            //login.VOOnlineUser = opUser.AddOnlineUser(info, login.VOUser.UserId);
            PowerCreator.LMS.Common.Script.Script.ReloadMe();
        }
        else
        {
            opUser.AddOnlineUser(info, PowerCreator.LMS.Web.User.User.LoginNoUserID);
            string msg = config.GetLanagerValueByKey(login.LoginMessage, PowerCreator.LMS.Language.LanagetItemType.MSG);
           
            PowerCreator.LMS.Common.Script.Script.Alert(this.Page, "al", msg);
        }
    }
    protected void btn_LoginOut_Click(object sender, EventArgs e)
    {
        PowerCreator.LMS.Web.User.User opUser = new PowerCreator.LMS.Web.User.User();
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] != null)
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login != null)
                opUser.UpdateOnlineUserByUserID(login.VOLearner.Learner_Id, 2);
        }

        Session.Abandon();
        PowerCreator.LMS.Common.Script.Script.ReloadMe();
    }
    protected void btn_Registor_Click(object sender, EventArgs e)
    {
        PowerCreator.LMS.Common.Script.Script.RedirectURL("Register.aspx");
    }
    protected void btn_Registor1_Click(object sender, EventArgs e)
    {
        PowerCreator.LMS.Common.Script.Script.RedirectURL("Register.aspx");
    }
    public string GetUserLocation()
    {
        try
        {
            WebClient webGetting = new WebClient();
            string userIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            string ipQueryResult = webGetting.DownloadString("http://www.ip.cn/getip.php?action=queryip&ip_url=" + userIP);
            string startString = @"来自：";
            int startIndex = ipQueryResult.LastIndexOf(startString) + startString.Length;
            int endIndex = ipQueryResult.LastIndexOf(@" ", startIndex);
            return ipQueryResult.Substring(startIndex, ipQueryResult.Length - startIndex);
        }
        catch
        {
            return string.Empty;
        }
    }
}
